#include "prioritylist.h"

PriorityList::PriorityList(int rMaxPriority, int rMinPriority):
    maxPriority(rMaxPriority),
    minPriority(rMinPriority)
{
}
PriorityList::~PriorityList()
{

}

void PriorityList::printPriorities() {
   // qDebug() << "Priorities:";
    for(elementListIterator eIt = list.begin(); eIt != list.end(); ++eIt) {
        qDebug() << (*eIt).priority;
    }
}
#include <sstream>
void PriorityList::printElements(void) {
   // qDebug() << "Priorities with elements:";
    for(elementListIterator eIt = list.begin(); eIt != list.end(); ++eIt) {
        std::ostringstream convert;
        convert << ((*eIt).priority);
        convert << (':');
        for(intListIterator iIt = (*eIt).elements.begin(); iIt != (*eIt).elements.end(); ++iIt) {
            convert << ((*iIt));
            convert << (' ');
        }
        qDebug() << QString::fromStdString(convert.str());
    }
}

void PriorityList::add(int rPriority, int rId) {
   // qDebug() << "add " << rPriority << rId;
    if(maxPriority < rPriority)
    {
        rPriority = maxPriority;
    }else if(minPriority > rPriority)
    {
        rPriority = minPriority;
    }

    element newElement;
    newElement.priority = rPriority;
    newElement.elements.push_back(rId);

    int n = list.size();
    //no items in vector
    if(n == 0)
    {
        //add one element
        list.push_back(newElement);
    }else
    {
        element *p0 = &list.at(0);
        element *pn = &list.at(n-1);

        //one element in vector
        if(p0->priority == pn->priority)
        {
            if(rPriority > p0->priority)
            {
                list.insert(list.begin(), newElement);
            }else if(rPriority == p0->priority)
            {
                p0->elements.push_back(rId);
            }else
            {
                list.push_back(newElement);
            }
        }else //more than one element in vector
        {
            for(elementListIterator eIt = list.begin(); eIt != list.end(); ++eIt)
            {
                if(rPriority > (*eIt).priority) {
                    list.insert(eIt, newElement);
                    break;
                }else if(rPriority == (*eIt).priority) {
                    (*eIt).elements.push_back(rId);
                    break;
                }else {
                   elementListIterator tIt = eIt;
                   tIt++;
                   if(tIt == list.end()) {
                       list.push_back(newElement);
                       break;
                   }else {
                       if(rPriority > (*tIt).priority) {
                           list.insert(tIt, newElement);
                           break;
                       }
                   }
                }
            }
        }
    }
    printElements();
}

void PriorityList::remove(int rId) {
    qDebug() << "remove " << rId;
    for(elementListIterator eIt = list.begin(); eIt != list.end(); ++eIt) {
        for(intListIterator iIt = (*eIt).elements.begin(); iIt != (*eIt).elements.end(); ++iIt) {
            if((*iIt)==rId) {
                (*eIt).elements.erase(iIt);
                if((*eIt).elements.size() <= 0) {
                    list.erase(eIt);
                }
                printElements();
                return;
            }
        }
    }
    printElements();
}

int PriorityList::pop(void) {
    //qDebug() << "PriorityList::pop";
    int id = -3;
    if(list.size() <= 0) {
        id = -1;
    }else {
        if(list.at(0).elements.size() <= 0) {
            id = -2;
        }else if(list.at(0).elements.size() == 1) {
            id = list.at(0).elements.at(0);
            list.erase(list.begin());
        }else {
            id = list.at(0).elements.at(0);
            list.at(0).elements.erase(list.at(0).elements.begin());
        }
    }

    printElements();

    return id;
}

int PriorityList::top(void) {
    //qDebug() << "PriorityList::top";
    int id = -3;
    if(list.size() <= 0) {
        id = -1;
    }else {
        if(list.at(0).elements.size() <= 0) {
            id = -2;
        }else {
            id = list.at(0).elements.at(0);
        }
    }

    printElements();

    return id;
}

int PriorityList::size(void) {
    int count = 0;
    for(elementListIterator eIt = list.begin(); eIt != list.end(); ++eIt) {
        count += (*eIt).elements.size();
    }
    return count;
}
